package TravelAgent;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.Iterator;

public class TravelAgentRmiClient {
    private static final String HOST = "192.168.3.100";
    private static final int PORT = 1099;
    //private static Registry registry;
    ArrayList<String> hotels;
    ArrayList<String> ticketMasters;
    ArrayList<String> activities;
    String randomActivity;

    public TravelAgentRmiClient() {
    	hotels = new ArrayList<String>();
    	hotels.add("Conrad"); hotels.add("Ciragan"); hotels.add("Divan");
    	ticketMasters = new ArrayList<String>();
    	ticketMasters.add("Biletix"); ticketMasters.add("Ticketturk"); 
    	activities.add("opera"); activities.add("theater");
    	randomActivity = activities.get(1);
	}
	
    public boolean askHotel(String beginDate, String endDate, String hotel, String people)
    						throws MalformedURLException, RemoteException, NotBoundException{
    	//Aktivite yoksa kafadan cik
    	if(!askActivity(beginDate, people)){
    		return false;
    	}
    	Reservable remoteApi = (Reservable) Naming.lookup("rmi://"+HOST+":"+PORT+"/"+hotel);
    	if (remoteApi.isAvailable(beginDate +":"+ endDate +":"+people)) {
			return true;
		} else {
			return false;
		}
    }
    
    public boolean askActivity(String beginDate, String people) 
    						throws MalformedURLException, RemoteException, NotBoundException{
    	for(String ticketMaster : ticketMasters){
    		Reservable remoteApi = (Reservable) Naming.lookup("rmi://"+HOST+":"+PORT+"/"+ticketMaster);
    		for(String activity : activities){
    			if(remoteApi.isAvailable(beginDate +":"+people+":"+activity)){
    				return true;
    			}
    		}
    	}
    	
    	return false;
    }
    
    public Itinerary[] findAlternatives(String beginDate, String endDate, String hotel, String people) throws MalformedURLException, RemoteException, NotBoundException{
    	ArrayList<Itinerary> alternatives = new ArrayList<Itinerary>();
    	Reservable remoteApi = (Reservable) Naming.lookup("rmi://"+HOST+":"+PORT+"/"+hotel);
    	hotels.remove(hotel);
		for(String hot : hotels	){
			remoteApi = (Reservable) Naming.lookup("rmi://"+HOST+":"+PORT+"/"+hot);
			if(remoteApi.isAvailable(beginDate +":"+ endDate +":"+people)){
				alternatives.add(new Itinerary( hotel, findRandomActivity(beginDate,people)));
			}
		}
		return (Itinerary[]) alternatives.toArray();
    }
    public String findRandomActivity(String beginDate, String people) throws MalformedURLException, RemoteException, NotBoundException{
    	for(String ticketMaster : ticketMasters){
    		Reservable remoteApi = (Reservable) Naming.lookup("rmi://"+HOST+":"+PORT+"/"+ticketMaster);
    		for(String activity : activities){
    			if(remoteApi.isAvailable(beginDate +":"+people+":"+activity)){
    				return activity;
    			}
    		}
    	}
    	return randomActivity;
    }
    public boolean makeReservation(String beginDate, String endDate, String hotel, String people) throws MalformedURLException, RemoteException, NotBoundException{
    	Reservable remoteApi = (Reservable) Naming.lookup("rmi://"+HOST+":"+PORT+"/"+hotel);
    	if(remoteApi.makeReservation(beginDate +":"+ endDate +":"+people)){
        	for(String ticketMaster : ticketMasters){
        		remoteApi = (Reservable) Naming.lookup("rmi://"+HOST+":"+PORT+"/"+ticketMaster);
        		for(String activity : activities){
            		if(remoteApi.makeReservation(beginDate +":"+people+":"+activity)){
            			return true;
            		}
        		}
        	}

    	}
    	
		return false;
    }
    
    public boolean makeSelectedReservation(String beginDate, String endDate, String hotel, String people, String activity) throws MalformedURLException, RemoteException, NotBoundException{
    	Reservable remoteApi = (Reservable) Naming.lookup("rmi://"+HOST+":"+PORT+"/"+hotel);
    	if(remoteApi.makeReservation(beginDate +":"+ endDate +":"+people)){
        	for(String ticketMaster : ticketMasters){
        		remoteApi = (Reservable) Naming.lookup("rmi://"+HOST+":"+PORT+"/"+ticketMaster);
        		if(remoteApi.makeReservation(beginDate +":"+people+":"+activity)){
        			return true;
        		}
        	}
    	}
    	
		return false;
    }
    
}
